package com.lfs.market.controller.front;

import com.lfs.market.job.MyJob;
import com.lfs.market.model.JobBean;
import com.lfs.market.utils.JobUtils;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lfs
 * @date 2025/6/30 17:33:38
 */
@RestController
@RequestMapping("/quartz")
public class QuartzController {
    @Autowired
    private Scheduler scheduler;
    private final String jobName = "myJob";


    @PostMapping("/create")
    public String createJob() {
        //corn： "0 0 1 * * ?"   每天凌晨1点执行
        JobBean jobBean = new JobBean("myJob", MyJob.class.getName(), "0/3 * * * * ? *");
        JobUtils.createJob(scheduler, jobBean);

        return "任务创建成功！";
    }

    @PostMapping("/pause")
    public String pauseJob() {
        JobUtils.pauseJob(scheduler, jobName);
        return "任务暂停成功！";
    }

    @PostMapping("/resume")
    public String resumeJob() {
        JobUtils.resumeJob(scheduler, jobName);
        return "任务恢复成功！";
    }

    @PostMapping("/delete")
    public String deleteJob() {
        JobUtils.deleteJob(scheduler, jobName);
        return "任务删除成功！";
    }

    @PostMapping("/once")
    public String runOnceJob() {
        JobUtils.runJobOnce(scheduler, jobName);
        return "任务执行一次就成功！";
    }

    @PostMapping("/modify")
    public String modifyJob() {
        JobBean jobBean = new JobBean(jobName, MyJob.class.getName(), "0/5 * * * * ? *");
        JobUtils.modifyJob(scheduler, jobBean);
        return "任务修改成功！";
    }
}
